package org.jeecg.modules.friends.utils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.lang3.StringUtils;

/**
 * Created on 2025-01-04
 */
public class ImageUtils {
    public static String getCorrectImageUrl(String picUlr) {
        // 创建URL对象
        try {
            URL url = new URL(picUlr);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("HEAD");
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                return picUlr;
            } else {
                return "https://friends-images.oss-cn-hangzhou.aliyuncs.com/static/weigui.png";
            }
        } catch (IOException e) {
            return picUlr;
        }
    }

    // 图片压缩
    public static String compressImage(String imageUrl) {
        if (StringUtils.isEmpty(imageUrl) || imageUrl.contains("?x-oss-process=image/resize")) {
            return imageUrl;
        }
        return imageUrl + "?x-oss-process=image/resize,m_lfit,h_600,w_600";
    }
}
